﻿using Furion.DynamicApiController;
using Furion.JsonSerialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace FurStudy.Application
{
    /// <summary>
    /// JSON测试类
    /// </summary>
    internal class JsonDemo : IDynamicApiController
    {
        private readonly IJsonSerializerProvider _jsonSerializer;
        private readonly IJsonSerializerProvider _jsonSerializer2;

        public JsonDemo(IJsonSerializerProvider jsonSerializer)
        {
            _jsonSerializer = jsonSerializer;
            _jsonSerializer2 = JSON.GetJsonSerializer();
        }

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <returns></returns>
        public string GetText()
        {
            return _jsonSerializer.Serialize(new 
            {
                Id = 1,
                Name = "Furion"
            },new JsonSerializerOptions 
            {
                WriteIndented = true // 格式化输出（如需配置更多选项，只需创建JsonSerializerOptions配置对象即可）
            });
        }

        /// <summary>
        /// 反序列化字符串
        /// </summary>
        /// <returns></returns>
        public object GetObject()
        {
            var json = "{\"Id\":1,\"Name\":\"Furion\"}";
            var obj = _jsonSerializer2.Deserialize<object>(json);

            // 单独配置序列化不区分大小写
            var objCaseInsensitive = _jsonSerializer.Deserialize<object>(json, new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true // 不区分大小写
            });

            return obj;
        }
    }
}
